//获取应用实例
const app = getApp()
const db = wx.cloud.database()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        formWarn: false,
        money: {
            time: "",//时间
            price: "",
            place: "",//地点
            intro: "", //信息
        },
        fileList: [],
        // date: '',
        show: false,
    },
    inputChange(e) {
        if (e.currentTarget.dataset.item == "place") {
            this.data.money.place = e.detail
        } else if (e.currentTarget.dataset.item == "intro") {
            this.data.money.intro = e.detail
        } else if (e.currentTarget.dataset.item == "price") {
            this.data.money.price = e.detail
        }
    },
    upload() {
        const that = this;
        wx.cloud.callFunction({
            name: 'money',
            data: {
                data: this.data.money
            }
        }).then((res0) => {
            // wx.hideLoading()
            wx.navigateTo({
                url: '/pages/homePage/money/moneydetail/moneydetail',
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('to_details', {
                        mes: res0.result,
                        status: "2"
                    })
                }
            })

        })
    },
    submit() {
        this.data.money.merchant = app.globalData.userInfo.nickName
        this.data.money.avatarUrl = app.globalData.userInfo.avatarUrl
        for (let item in this.data.money) {
            if (!this.data.money[item]) {
                // console.log(item)
                this.setData({
                    formWarn: true
                })
                return setTimeout(() => {
                    this.setData({
                        formWarn: false
                    })
                }, 2000)
            }
        }
        wx.showLoading({
            mask: true,
            title: '上传中',
        })
        this.upload()
    },

    //time
    onDisplay() {
        this.setData({ show: true });
    },
    onClose() {
        this.setData({ show: false });
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    },
    onConfirm(event) {
        this.setData({
            show: false,
            'money.time': this.formatDate(event.detail),
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})